package thread;

import java.util.Scanner;

public class ThreadSafe {
    public volatile static int count = 0;
    public static void main1(String[] args) throws InterruptedException {
        Object o = new Object();
        Thread t1 = new Thread(()->{
            for (int i = 0; i < 50000; i++) {
                synchronized (o) {
                    count++;
                }
            }
        });
        Thread t2 = new Thread(()->{
            for (int i = 0; i < 50000; i++) {
                synchronized (o) {
                    count++;
                }
            }
        });
        t1.start();
        t1.join();
        t2.start();
        t2.join();
        System.out.println(count);
    }
    // 一个变量读 一个变量修改
    public static void main(String[] args) {
        Thread t1 = new Thread(()->{
            while (count == 0){

            }
            System.out.println("t1 线程结束");
        });
        t1.start();
        Thread t2 = new Thread(()->{
            System.out.println("请输入count");
            Scanner s = new Scanner(System.in);
            count = s.nextInt();
            // 用户输入非0 t1会停止 输出结束
        });
        t2.start();  // 内存可见性引起
    }
}
